record = {0:1,4:1,6:1,9:1,8:2}
def count(num):
    nn = num
    cnt = 0
    while nn:
        cnt += record.get(nn%10,0)
        nn //= 10
    return cnt,num

n = int(input())
nums = list(map(int,input().split()))
nums.sort(key=count)
print(" ".join(map(str, nums)))